package com.lvwenguo.douyu.controller;

import com.lvwenguo.douyu.entity.UserFile;
import com.lvwenguo.douyu.service.DouyuService;
import com.lvwenguo.returnresult.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author lvwenguo
 * @Date 2021/4/4 11:08
 * @Version 1.0
 */
@RestController
public class DouyuController {

    @Autowired
    private Result result;
    @Autowired
    private DouyuService douyuService;
    /**
     * 模仿斗鱼，文件上传接口
     * @param file
     * @param req
     */
    @PostMapping("/fileupload")
    public Result fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
        if(douyuService.fileUpload(file, req)){
            return result.ok();
        }
        return result.error();
    }

    @GetMapping("/getfilePath")
    public Result getfilePath(HttpServletRequest req){
        UserFile userFile = douyuService.getfilePath(req);
        if(!ObjectUtils.isEmpty(userFile)){
            return result.ok(userFile);
        }
        return result.error();
    }

}
